var participants = ["Eyal", "Roee", "Gil", "Yanir"];
var rand_titles = ["Try iPhone Facebook app contact/photos sync",
                   "Google Workshop 'Point and Click' Presentation",
                   "Programming Assignment (summary)",
                   "Add your project's screenshots to your Google",
                   "Buy a new toothbrush",
                   "Note: Participant information can be dynamically updated (except for IDs). This includes the thumbnail URL, display name, and any future extensions to the participant. Instead of storing this information, gadgets should update displayed participant data each time they receive a participant callback."];
var title_num = 0;
/*
 * Randomizes a task
 */
function rand_task(notCompleted){

    var t = new task();
    var due = new Date();
    //due.setFullYear(2010, 1, 1);
    due.setDate(due.getDate()+irand(20)-8); //future and past
    
//    if(irand(3) == 1) // 1 out of 5
//        due = null;
    
    
    t.id = "task" + irand(9999);
    t.title = rand_titles[(title_num++)%rand_titles.length];
    t.priority = irand(4);
    t.dueDate = due;
    t.creator = participants[irand(4)-1];
    t.location = [null, null, "Tel Aviv University", "Ashdod", "Dizingof center"][irand(5)-1];
    t.tags = [[],[],[], ["call", "errand"], ["personal"],[], ["email", "personal"]][irand(7)-1];
    t.lastModifier = t.creator;
    t.lastModified = new Date();
    
    t.addedDate = new Date();
    t.link = ['', 'http://bing.com#:)'][irand(2)-1];
    t.progress = irand(100);
    t.status = ['', 'done'][irand(2)-1];
    t.owner = (irand(2)-1) ? participants[irand(4)-1] : '';
    t.freq = ["every 1 day", "monthly", "after 2 weeks", ''][irand(4)-1];
    t.dur = ["20 min", "1 hr", "5 hours", ''][irand(4)-1];
    
    if(!notCompleted && irand(4) == 1){ // 1 out of 4
        t.completedDate = new Date(); // arbitrary date
        t.completer = t.creator;
    }
    
    return t;
}

/*
 * randomize an integer from 1 to n
 */
function irand(n){
        return  Math.floor(Math.random()*n)+1;
}

function randAction(numOfActions) {
    if(!numOfActions)
        numOfActions = 1;
    
    
    var addedTasks = [];
    var updatedTasks = [];
    var completedTasks = [];
    var deletedTasks = [];
    
    while(numOfActions-- > 0)
    {
		var n = visible_tasks.length;
        var rand_existing_task = (n > 0) ? visible_tasks[irand(n)-1] : null;
        
        var action = (n > 0) ? irand(3) : 1;
        
        switch (action) {
                case 1: // Add a new task
                        addedTasks.push(rand_task());
                        break;
                case 2: // Remove/complete a task
                        rand_existing_task.lastModifier = participants[irand(4)-1];
                        rand_existing_task.lastModified = new Date();
                        
                        if(irand(2)==1)
                                completedTasks.push(rand_existing_task);
                        else
                                deletedTasks.push(rand_existing_task);
                        break;
                case 3: // update a task
                        var new_task = rand_task(true);
                        new_task.id = rand_existing_task.id; // replace old task data
                        
                        updatedTasks.push(new_task);
        }
    }
    
    gui_update_callback(addedTasks, updatedTasks, completedTasks, deletedTasks);
}

var robotEnabled = false;

$(document).ready(function(){
        if(!robotEnabled)
                return;
        
        setInterval ( "randAction(irand(1))", 10000 );
});
